package controllers

import java.io.File

import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 11/5/2020
 */
class DownloadController @Inject()(cc: ControllerComponents) extends  AbstractController(cc){

  def toIndex = Action { implicit request =>
    Ok(views.html.download.index())
  }

  def downloadData = Action { implicit request =>
    val data = FormTool.fileNameForm.bindFromRequest().get
    val file = new File(Tool.downloadDir, data.fileName)
    Ok.sendFile(file).withHeaders(
      CONTENT_DISPOSITION -> s"attachment; filename=${
        file.getName
      }",
      CONTENT_TYPE -> "application/x-download"
    )
  }



}
